//
//  UIBarButtonItem+Extension.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/3.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

+ (instancetype)itemWithImageName:(NSString *)imgName target:(id)target action:(SEL)action{
    
    UIButton *button = [[UIButton alloc]init];
    
    [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imgName]] forState:UIControlStateHighlighted];
    
    button.size = button.currentImage.size;
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc]initWithCustomView:button];
}


+ (instancetype)itemWithImageName:(NSString *)imgName title:(NSString *)title target:(id)target action:(SEL)action{
    
    UIButton *button = [[UIButton alloc]init];
    
    [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted",imgName]] forState:UIControlStateHighlighted];
    

    //设置title
    [button setTitle:title forState:UIControlStateNormal];
    //设置 title 不同状态的 title 的颜色
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor colorWithRed:53/255.0 green:53/255.0 blue:53/255.0 alpha:1.0] forState:UIControlStateNormal];
    
    //根据内容调整大小
    [button sizeToFit];
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return [[UIBarButtonItem alloc]initWithCustomView:button];
    
}


@end
